﻿using System.Net;

namespace ST_CSharp_IO操作
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //PathDemo();
            //DirectoryDemo();
            //FileDemo();
            //FileInfoDemo();

            // IPHostEntry iP = Dns.GetHostEntry("https://www.baidu.com/");
        }

        /// <summary>
        /// Path类
        /// 路径
        /// </summary>
        public static void PathDemo()
        {
            string path1 = "E:\\WorkSpace\\IO测试";

            // 修改Path字符串的后缀名
            string newPath1 = Path.ChangeExtension(path1, "doc");

            string path2 = "p1\\test.txt";

            // 连接多个字符串组个成一个新的Path
            string newPath2 = Path.Combine(path1, path2);

            // 获取后缀名
            string ext = Path.GetExtension(path2);

            // 获取完整文件名
            string fileName = Path.GetFileName(path2);

            // 获取文件名（无后缀）
            string fileName2 = Path.GetFileNameWithoutExtension(path2);

            // 判断是否包含后缀名
            bool b = Path.HasExtension(path2);

            // 获取绝对路径Path
            string fullPath = Path.GetFullPath(path2);
        }

        /// <summary>
        /// Directory类
        /// 目录
        /// </summary>
        public static void DirectoryDemo()
        {
            string path1 = "E:\\WorkSpace\\IO测试\\p1";
            // 判断目录是否存在
            if (!Directory.Exists(path1))
            {
                // 创建目录
                Directory.CreateDirectory(path1);
            }
            string path2 = "E:\\WorkSpace\\IO测试\\p3";
            if (Directory.Exists(path2))
            {
                // 删除目录及其子目录,不传第二个参数true则不删除子目录（有子目录就报错）
                Directory.Delete(path2, true);
            }
            // 移动当前目录到指定目录（当前目录会被删除，指定目录必须不存在）
            Directory.Move("E:\\WorkSpace\\IO测试\\p1", "E:\\WorkSpace\\IO测试\\p3");
            string path4 = "E:\\WorkSpace\\IO测试\\p4";
            if (!Directory.Exists(path4))
            {
                // 创建目录
                Directory.CreateDirectory(path4);
            }
            // 获取子目录列表
            string[] dirs = Directory.GetDirectories(path4);
            // 获取目录下的文件列表
            string[] files = Directory.GetFiles(path4);

            List<string> dicList = new List<string>();
            string path5 = "E:\\WorkSpace\\IO测试";
            GetChildDirectoiresAndFiles(path5, dicList);
        }

        /// <summary>
        /// DirectoryInfo类
        /// </summary>
        public static void DirectoryInfoDemo()
        {
            string path = "E:\\WorkSpace\\IO测试\\p5";
            DirectoryInfo directory = new DirectoryInfo(path);
            // 创建
            directory.Create();
            // 移动
            directory.MoveTo("E:\\WorkSpace\\IO测试\\p6");
            // 删除(false指只有空目录才能删)
            directory.Delete(false);
        }

        /// <summary>
        /// 递归获取目录下所有子目录及文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="dicList"></param>
        public static void GetChildDirectoiresAndFiles(string path, List<string> dicList)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            DirectoryInfo[] dirs = dir.GetDirectories();
            FileInfo[] files = dir.GetFiles();
            dicList.AddRange(files.Select(f => f.FullName));
            foreach (var item in dirs)
            {
                dicList.Add(item.FullName);
                GetChildDirectoiresAndFiles(item.FullName, dicList);
            }
        }

        /// <summary>
        /// File类
        /// </summary>
        public static void FileDemo()
        {
            string path1 = "E:\\WorkSpace\\IO测试\\p3\\file.txt";
            string path2 = "E:\\WorkSpace\\IO测试\\p3\\file2.txt";
            // 创建
            File.Create(path1);
            File.Create(path2, 1024);
            File.Create(path1, 1024, FileOptions.None);

            // 删除
            File.Delete(path1);

            // 复制
            File.Copy(path1, path2);

            // 移动
            File.Move(path1, path2);

            // 追加文本
            StreamWriter sw = File.AppendText(path1); // 存在则追加，不存在则创建
            File.AppendAllText(path1, "这是追加的内容");
            List<string> list = new List<string>();
            list.Append("1");
            list.Append("2");
            list.Append("3");
            list.Append("4");
            File.AppendAllLines(path1, list);

            // 打开文件
            FileStream fs1 = File.OpenRead(path1);  // 只读文件流
            FileStream fs2 = File.OpenWrite(path1);  // 写入文件流
            StreamReader sr = File.OpenText(path1);  // 打开文本文件
            FileStream fs3 = File.Open(path1, FileMode.Create);

            // 读取
            IEnumerable<string> listr = File.ReadLines(path1);
            string ss = File.ReadAllText(path1);
            string[] ssArr = File.ReadAllLines(path1);
            byte[] bytes = File.ReadAllBytes(path1);

            // 写入
            File.WriteAllText(path1, "adsfasdfsad");
            File.WriteAllBytes(path1, bytes);
            File.WriteAllLines(path1, ssArr);
        }

        /// <summary>
        /// FileInfo类
        /// </summary>
        public static void FileInfoDemo()
        {
            string path1 = "E:\\WorkSpace\\IO测试\\p3\\file.txt";
            string path2 = "E:\\WorkSpace\\IO测试\\p3\\file2.txt";
            FileInfo fileInfo1 = new FileInfo(path1);
            // 创建
            fileInfo1.Create();
            fileInfo1.CreateText();

            // 删除
            fileInfo1.Delete();

            // 复制
            fileInfo1.CopyTo(path2);

            // 移动
            fileInfo1.MoveTo(path2);

            // 追加文本
            StreamWriter sw = fileInfo1.AppendText();
        }
    }
}